
#include <stdio.h>
#include <winsock2.h>
#include "../CommonUtil/messages.h"
#include "../CommonUtil/UtilService.h"
#pragma comment (lib, "Ws2_32.lib")

int main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int wsaerr;

	// Using MAKEWORD macro, Winsock version request 2.2
	wVersionRequested = MAKEWORD(2, 2);

	wsaerr = WSAStartup(wVersionRequested, &wsaData);                        
	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 )
	{
		printf("The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
		WSACleanup();
		return 0;
	}

	int hsock = socket(AF_INET, SOCK_STREAM, 0);
	if(hsock == -1){
		printf("Error initializing socket %d\n",WSAGetLastError());
	}

	struct sockaddr_in my_addr;

	my_addr.sin_family = AF_INET ;
	my_addr.sin_port = htons(2222);

	memset(&(my_addr.sin_zero), 0, 8);
	my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR )
		fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError());
	
	IMsgSplitter* pSplitter = CreateMsgSplitter();

	//send register message
// 	MSG_USER_AUTH regMsg;
// 	strcpy(regMsg.szAccount, "HaixiaoYang");
// 	strcpy(regMsg.szPasswd, "hello world");
// 
// 	SendMessage(&regMsg, hsock);
// 
// 	int nLen = 0;
// 	char* p = pSplitter->GetRecvBuf(nLen);
// 	nLen = recv(hsock, p, nLen, 0);
// 	pSplitter->MsgReceived(nLen);
// 	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();

	//select route
// 	MSG_ROUTE_SEL selMsg;
// 	strcpy(selMsg.depTimeBeg, "2012-05-02 19:53");
// 	strcpy(selMsg.depTimeEnd, "2012-05-03 23:53");
// 	strcpy(selMsg.strStartAddr, "San Jose");
// 	strcpy(selMsg.strDestination, "Santa Clara");
// 	selMsg.nCabinType = CABINE_BUSINESS;
// 	SendMessage(&selMsg, hsock);
// 
// 	nLen = 0;
// 	p = pSplitter->GetRecvBuf(nLen);
// 	nLen = recv(hsock, p, nLen, 0);
// 	pSplitter->MsgReceived(nLen);
// 	MSG_ROUTE_REPLY* pRouteMsg = (MSG_ROUTE_REPLY*)pSplitter->ExtractMsg();
// 
// 	//reserve order
// 	MSG_CUST_RESERV resvMsg;
// 	resvMsg.nRouteNum = 8;
// 	resvMsg.nCabTp = CABINE_FIRSTCLASS;
// 	SendMessage(&resvMsg, hsock);
// 
// 	nLen = 0;
// 	p = pSplitter->GetRecvBuf(nLen);
// 	nLen = recv(hsock, p, nLen, 0);
// 	pSplitter->MsgReceived(nLen);
// 	pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();
// 
// 	//view order request
// 	MSG_VIEW_ORDER_REQUEST viewOrderMsg;
// 	SendMessage(&viewOrderMsg, hsock);
// 	nLen = 0;
// 	p = pSplitter->GetRecvBuf(nLen);
// 	nLen = recv(hsock, p, nLen, 0);
// 	pSplitter->MsgReceived(nLen);
// 	MSG_VIEW_ORDER_CONTENT* pOrdersMsg = (MSG_VIEW_ORDER_CONTENT*)pSplitter->ExtractMsg();

	//Cancel an order
// 	MSG_CANCEL_ORDER cancelMsg;
// 	cancelMsg.nReservId = 100;
// 	SendMessage(&cancelMsg, hsock);
// 
// 	nLen = 0;
// 	p = pSplitter->GetRecvBuf(nLen);
// 	nLen = recv(hsock, p, nLen, 0);
// 	pSplitter->MsgReceived(nLen);
// 	pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();

	//air line login
	MSG_AIRLINE_AUTH authMsg;
	strcpy(authMsg.szAccount, "American Airline");
	strcpy(authMsg.szPasswd, "yhx#1234");

	SendMessage(&authMsg, hsock);

	int nLen = 0;
	char* p = pSplitter->GetRecvBuf(nLen);
	nLen = recv(hsock, p, nLen, 0);
	pSplitter->MsgReceived(nLen);
	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();

	MSG_VIEW_ORDER_REQUEST viewOrderMsg;
	SendMessage(&viewOrderMsg, hsock);
	nLen = 0;
	p = pSplitter->GetRecvBuf(nLen);
	nLen = recv(hsock, p, nLen, 0);
	pSplitter->MsgReceived(nLen);
	MSG_VIEW_ORDER_CONTENT* pOrdersMsg = (MSG_VIEW_ORDER_CONTENT*)pSplitter->ExtractMsg();

	//true
	MSG_CANCEL_ORDER cancelMsg;
	cancelMsg.nReservId = 6;
	SendMessage(&cancelMsg, hsock);
	nLen = 0;
	p = pSplitter->GetRecvBuf(nLen);
	nLen = recv(hsock, p, nLen, 0);
	pSplitter->MsgReceived(nLen);
	pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();

	//false
	cancelMsg.nReservId = 8;
	SendMessage(&cancelMsg, hsock);
	nLen = 0;
	p = pSplitter->GetRecvBuf(nLen);
	nLen = recv(hsock, p, nLen, 0);
	pSplitter->MsgReceived(nLen);
	pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();

	//add route
	MSG_AIRLINE_ADDROUTE addMsg;
	strcpy(addMsg.entry.depTime, "2012-05-02 19:53");
	strcpy(addMsg.entry.arvTime, "2012-05-03 01:53");
	addMsg.entry.nBusiness = 123;
	addMsg.entry.nEconomy = 123;
	addMsg.entry.nFirstClass = 123;
	strcpy(addMsg.entry.strAirLine, "China Airline");
	strcpy(addMsg.entry.strFlightID, "ZX132");
	strcpy(addMsg.entry.strStartAddr, "Beijing");
	strcpy(addMsg.entry.strDestination, "New York");

	SendMessage(&addMsg, hsock);
	nLen = 0;
	p = pSplitter->GetRecvBuf(nLen);
	nLen = recv(hsock, p, nLen, 0);
	pSplitter->MsgReceived(nLen);
	pSucMsg = (MSG_SUCCESS_REPLY*)pSplitter->ExtractMsg();

	return 0;
}